package com.imooc.test;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class JasyptTestIgnore {

    /**
     * 加密
     */
    @Test
    public void testPwdEncrypt() {

        // 1. 实例化加密器
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        // 2. 配置加密算法和密钥
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setPassword("guoqitong");            // 用于加密的密钥（盐）
        config.setAlgorithm("PBEWithMD5AndDES");    // 设置加密算法

        encryptor.setConfig(config);
        // 3. 对密码进行加密

        String myPwd = "root";
        String encrypted = encryptor.encrypt(myPwd);

        System.out.println("+++++++++++++++++++++");
        System.out.println("原密码为：" + myPwd);
        System.out.println("加密后为：" + encrypted);
        System.out.println("+++++++++++++++++++++");

    }

    @Test
    public void testPwdDecrypt() {

        // 1. 实例化加密器
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        // 2. 配置加密算法和密钥
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setPassword("guoqitong");            // 用于加密的密钥（盐）
        config.setAlgorithm("PBEWithMD5AndDES");    // 设置加密算法

        encryptor.setConfig(config);


        String pendingPwd = "rKxFznUJtOtWLbe7r33XxQ==";
        String myPwd = encryptor.decrypt(pendingPwd);

        System.out.println("+++++++++++++++++++++");
        System.out.println("解密后为：" + myPwd);
        System.out.println("+++++++++++++++++++++");

        // G4uIW+yOXGFRM1XUH1JTow==
        // rKxFznUJtOtWLbe7r33XxQ==
    }
}
